package com.bxs.command;


import com.bxs.IdsManager;
import com.bxs.command.service.InterfacesService;
import com.bxs.global.CommandMsg;
import com.bxs.global.JsonResult;
import com.bxs.global.JsonUtils;
import com.bxs.pojo.NetWorkInterfaceInfo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.pcap4j.core.PcapNativeException;


import java.io.*;
import java.net.Socket;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommandHandlerThread implements Runnable{
    private Socket socket;
    private IdsManager idsManager;

    public CommandHandlerThread(Socket socket, IdsManager idsManager){
        this.socket = socket;
        this.idsManager = idsManager;
    }

    @Override
    public void run() {
        // 根据命令执行相应操作
        try{

            InputStream inputStream = socket.getInputStream();
            OutputStream outputStream = socket.getOutputStream();
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));

            // 读出命令数据
            int len;
            char buf[] = new char[1024];
            StringBuffer receiveData = new StringBuffer();

            // 读出命令数据
            System.out.println("start read");
            while(bufferedReader.ready() && (len = bufferedReader.read(buf, 0, 1024)) != -1) {     // 确保socket的缓冲区可读，否则会出现无限阻塞的情况
                System.out.println("read data len = " + len);
                receiveData.append(buf, 0, len);
            }
            ObjectMapper objectMapper = new ObjectMapper();
            CommandMsg commandMsg = objectMapper.readValue(receiveData.toString(), CommandMsg.class);
            System.out.println(commandMsg);
            // 执行相应操作
            if(commandMsg.getCmd().equals("test")){             // 测试操作
                // TODO，具体操作
                // 给出处理结果
                Map<String, Object> data = new HashMap<>();
                data.put("code", 200);
                data.put("msg", "success");
                String s = objectMapper.writeValueAsString(data);
                bufferedWriter.write(s);
            }else if(commandMsg.getCmd().equals("sys_info")){   // 获取系统运行信息
                //
            }else if(commandMsg.getCmd().equals("interfaces_info")){   // 获取可用网卡信息
                //
                InterfacesService interfacesService = new InterfacesService();
                List<NetWorkInterfaceInfo> allInterfaces = interfacesService.getAllInterfaces();
                JsonResult resp = JsonResult.success(allInterfaces);
                System.out.println(resp);
                String s = JsonUtils.toJsonData(resp);
                System.out.println(s);
                bufferedWriter.write(s);
            }else{
                // 默认响应
                bufferedWriter.write(JsonUtils.toJsonData(JsonResult.success("success")));
            }


            bufferedWriter.flush();       // 确保关闭之前将该写的数据都写到socket缓冲中
            bufferedReader.close();
            bufferedWriter.close();
            outputStream.close();
            inputStream.close();
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        } catch (IOException | PcapNativeException e) {
            e.printStackTrace();
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
